package com.example.administrator.yuejian35.Club.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian35.Club.adapter.CoachListAdapter;
import com.example.administrator.yuejian35.Club.bean.Club;
import com.example.administrator.yuejian35.Club.bean.Coach;
import com.example.administrator.yuejian35.Club.bean.Guanzhu;
import com.example.administrator.yuejian35.Club.bean.Photo;
import com.example.administrator.yuejian35.Club.model.ClubModel;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.base.BaseActivity;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.administrator.yuejian35.Config.Img_URL;

public class ClubInfoActivity extends Activity {
    private ImageView pic1;
    private ImageView pic2;
    private ImageView pic3;
    private ImageView ic_back;
    private TextView club_name;
    private TextView club_intro;
    private TextView club_address;
    private TextView club_phone;
    private TextView club_time;
    private String phonenum;
    private TextView club_equ;
    private ImageView callw;
    private List<Coach> list;
    private RecyclerView coach_rv;
    private LinearLayout course;
    private LinearLayout video;
    private ImageView guanzhu_button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_info);
        pic1 = (ImageView) findViewById(R.id.pic1);
        pic2 = (ImageView) findViewById(R.id.pic2);
        pic3 = (ImageView) findViewById(R.id.pic3);
        ic_back = (ImageView) findViewById(R.id.video_pic);
        club_name = (TextView) findViewById(R.id.club_name);
        club_intro = (TextView) findViewById(R.id.club_intro);
        club_address = (TextView) findViewById(R.id.club_address);
        club_time = (TextView) findViewById(R.id.club_time);
        club_equ = (TextView) findViewById(R.id.club_equ);
        club_phone = (TextView) findViewById(R.id.club_phone);
        callw = (ImageView) findViewById(R.id.callphone);
        coach_rv = (RecyclerView) findViewById(R.id.coach_rv);
        guanzhu_button = (ImageView) findViewById(R.id.guan);
        course = (LinearLayout) findViewById(R.id.course);
        video = (LinearLayout) findViewById(R.id.video);
        final String uid= BaseActivity.getUserId();
        final String clubId = getIntent().getStringExtra("clubid");
        ic_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        ClubModel clubmodel1 = new ClubModel();
        Call call2 = clubmodel1.getAllCoach(clubId);
        call2.enqueue(new Callback<List<Coach>>() {
            @Override
            public void onResponse(Call<List<Coach>> call, Response<List<Coach>> response) {
                if(ClubInfoActivity.this!=null) {
                    list = response.body();
                    CoachListAdapter adapter = new CoachListAdapter(ClubInfoActivity.this, list);
                    GridLayoutManager layoutManager = new GridLayoutManager(ClubInfoActivity.this,5);
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//Horizontal
                    coach_rv.setLayoutManager(layoutManager);
                    coach_rv.setAdapter(adapter);
                    coach_rv.setItemAnimator(new DefaultItemAnimator());
                }
            }

            @Override
            public void onFailure(Call<List<Coach>> call, Throwable t) {

            }
        });

        guanzhu_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClubModel clubmodel = new ClubModel();
                Call call1 = clubmodel.guanzhuclub(clubId,uid);
                call1.enqueue(new Callback<Guanzhu>() {
                    @Override
                    public void onResponse(Call<Guanzhu> call, Response<Guanzhu> response) {
                        Toast.makeText(ClubInfoActivity.this,"关注成功！", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onFailure(Call<Guanzhu> call, Throwable t) {

                    }
                });

            }
        });
       // Toast.makeText(ClubInfoActivity.this,clubId,Toast.LENGTH_SHORT).show();
        ClubModel clubmodel = new ClubModel();
        Call call = clubmodel.getclubphoto(clubId);
        call.enqueue(new Callback<List<Photo>>() {
            @Override
            public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
                if(!response.body().get(0).getPic().equals("-1")){
                    Picasso.with(ClubInfoActivity.this)
                            .load(Img_URL+response.body().get(0).getPic())
                            .into(pic1);
                    Picasso.with(ClubInfoActivity.this)
                            .load(Img_URL+response.body().get(1).getPic())
                            .into(pic2);
                    Picasso.with(ClubInfoActivity.this)
                            .load(Img_URL+response.body().get(2).getPic())
                            .into(pic3);
                }

            }

            @Override
            public void onFailure(Call<List<Photo>> call, Throwable t) {

            }
        });

        Call call1 = clubmodel.getclub(clubId);
        call1.enqueue(new Callback<Club>() {
            @Override
            public void onResponse(Call<Club> call, Response<Club> response) {
                club_name.setText(response.body().getClubname().toString());
                club_intro.setText(response.body().getClubintro().toString());
                club_address.setText(response.body().getAddress().toString());
                club_time.setText(response.body().getOpentime().toString());
                club_phone.setText(response.body().getPhone().toString());
                club_equ.setText(response.body().getEquipsuggestion().toString());
                phonenum=response.body().getPhone().toString();
            /*    callw.setText("点击拨打:"+response.body().getPhone().toString());*/
               /* club_name.setText(response.body().getClubname().toString());
                club_name.setText(response.body().getClubname().toString());*/
            }

            @Override
            public void onFailure(Call<Club> call, Throwable t) {

            }
        });

        course.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ClubInfoActivity.this,CourseListActivity.class);
                intent.putExtra("clubid",clubId);
                ClubInfoActivity.this.startActivity(intent);
            }
        });

        video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ClubInfoActivity.this,VideoListActivity.class);
                intent.putExtra("clubid",clubId);
                ClubInfoActivity.this.startActivity(intent);
            }
        });


        /*club_tel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phonenum));

                if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
            }
        });
*/

        callw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phonenum));

                if (ActivityCompat.checkSelfPermission(ClubInfoActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
            }
        });

    }


}
